package com.sunday.common.mybatis.plus.study.e2_Wrapper;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.sunday.common.leaf.config.LeafProperties;
import com.sunday.common.leaf.core.IdWorker;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Slf4j
@Configuration
@MapperScan("com.sunday.common.mybatis.plus.study.tool.db")
public class MybatisPlusConfig implements EnvironmentAware {

    private Environment env;

    private Binder binder;

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        /**
         * 分页
         */
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.env = environment;
        binder = Binder.get(this.env);
    }

    @PostConstruct
    public void idWorker() {
        LeafProperties leafProperties = binder.bindOrCreate("sunday.leaf.snowflake", LeafProperties.class);
        IdWorker.build(leafProperties);
    }

}